import { Injectable } from '@angular/core';
import { AppConfig } from "../config";
import { Http, Headers, RequestOptions } from "@angular/http";
import { Observable } from "rxjs/Observable";
import { DatePipe } from "@angular/common";
import { WindPowerPairs, actualWPPairs } from "../entity/wind-power-pairs.model";
import { WindPowers } from "../entity/wind-powers.model";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class WindPowerDrawService {

  private fomat: string = "yyyy-MM-dd HH:mm:ss";
  //功率曲线(点)
  private readonly baseUrlPer = `${AppConfig.APP_RESTFUL}`;
  private readonly param = "performance";
  private readonly url = `${this.baseUrlPer}/${this.param}`;

  //风机id获取设计曲线
  private readonly baseUrl = `${AppConfig.APP_RESTFUL}`;
  private readonly paramLine = "windPowers";
  private readonly urlLine = `${this.baseUrl}/${this.paramLine}`;

  /**
   * 登录进度
   * 
   * @private
   * @type {boolean}
   * @memberof AuthService
   */
  private _drawProgress: boolean;

  constructor(
    private http: InterceptorService,
    private datePipe: DatePipe) { }

  /**
   * 数据请求进度
   * true：请求结束，false：请求正在进行，未结束
   * @readonly
   * @type {boolean}
   * @memberOf AuthService
   */
  get drawProgress(): boolean {
    return this._drawProgress;
  }


  //根据风机id获取设计曲线
  getDesignWindPowerByTurbineId(deviceId: number): Promise<WindPowers[]> {

    return this.http.get(`${this.urlLine}/deviceId/${deviceId}`)
      .toPromise()
      .then(response => {
        let resault = response.json();
        try {
          switch (resault.code) {
            case "200":
              return resault.data as WindPowers[];
            case "-1":
              throw new Error(resault.message);
          }
        }
        catch (error) {
          throw error;
        }
      })
      .catch(this.handleError);
  }

  //曲线绘制方法；
  drawWindPowerDataByTurbineId(deviceId: number, startTime: Date, endTime: Date): Promise<WindPowerPairs> {
    /* 进度条显示 */
    this._drawProgress = true;

    let startFomat: string = "";
    let endFomat: string = "";
    if (deviceId == null)
      return Promise.reject("请选择设备！");
    startFomat = this.datePipe.transform(startTime, this.fomat);
    endFomat = this.datePipe.transform(endTime, this.fomat);

    return this.http.get(`${this.url}/deviceId/${deviceId}/startTime/${startFomat}/endTime/${endFomat}`)
      .toPromise()
      .then(response => {
        let resault = response.json();
        try {
          switch (resault.code) {
            case "200":
              /* 进度条消失 */
              this._drawProgress = false;
              return resault.data as WindPowerPairs;
            case "-1":
              this._drawProgress = false;
              throw new Error(resault.message);
          }
        }
        catch (error) {
          throw error;
        }
      })
      .catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    return Promise.reject(error.message || error);
  }

}



// WEBPACK FOOTER //
// ./src/app/service/wind-power-draw.service.ts